/**
 * 初始化${moduleName}详情对话框
 */
var ${entity}InfoDlg = {
    ${package.Parent}InfoData: {},
    validateFields: {
    <#list table.fields as field>
        <#if field.propertyName == "sort">
        sort: {
            validators: {
                notEmpty: {
                    message: '排序不能为空'
                }
            }
        },
        </#if>
        <#if field.propertyName == "status">
        status: {
            validators: {
                notEmpty: {
                    message: '状态不能为空'
                }
            }
        },
        </#if>
    </#list>
    }
};

/**
 * 清除数据
 */
${entity}InfoDlg.clearData = function () {
 this.${package.Parent}InfoData = {};
}

/**
 * 设置对话框中的数据
 *
 * @param key 数据的名称
 * @param val 数据的具体值
 */
${entity}InfoDlg.set = function (key, val) {
    this.${package.Parent}InfoData[key] = (typeof val == "undefined") ? $("#" + key).val() : val;
    return this;
}

/**
 * 获取对话框中的数据
 *
 * @param key 数据的名称
 * @param val 数据的具体值
 */
${entity}InfoDlg.get = function (key) {
    return $("#" + key).val();
}

/**
 * 关闭此对话框
 */
${entity}InfoDlg.close = function () {
    parent.layer.close(window.parent.${entity}.layerIndex);
}

/**
 * 收集数据
 */
${entity}InfoDlg.collectData = function () {
    this
<#list table.fields as field>
    <#if field.keyFlag>
    .set('id')
    <#elseif !field.keyFlag>
    .set('${field.propertyName}')
    </#if>
    <#if !field?has_next>
    ;
    </#if>
</#list>
}

/**
 * 校验数据
 */
${entity}InfoDlg.validate = function () {
    $('#${package.Parent}').data("bootstrapValidator").resetForm();
    $('#${package.Parent}').bootstrapValidator('validate');
    return $("#${package.Parent}").data('bootstrapValidator').isValid();
}

/**
 * 提交添加
 */
${entity}InfoDlg.addSubmit = function () {
    this.clearData();
    this.collectData();
    if (!this.validate()) {
        return false;
    }
    //提交信息
    const ajax = new $ax(Feng.ctxPath + "/${package.Parent}/add", function (data) {
        Feng.success("添加成功!");
        window.parent.${entity}.table.refresh();
        ${entity}InfoDlg.close();
    }, function (data) {
        Feng.error("添加失败!" + data.responseJSON.message + "!");
    });
    ajax.set(this.${package.Parent}InfoData);
    ajax.start();
}

/**
 * 提交修改
 */
${entity}InfoDlg.editSubmit = function () {
    this.clearData();
    this.collectData();
    if (!this.validate()) {
        return false;
    }
    //提交信息
    const ajax = new $ax(Feng.ctxPath + "/${package.Parent}/update", function (data) {
        Feng.success("修改成功!");
        window.parent.${entity}.table.refresh();
        ${entity}InfoDlg.close();
    }, function (data) {
        Feng.error("修改失败!" + data.responseJSON.message + "!");
    });
    ajax.set(this.${package.Parent}InfoData);
    ajax.start();
}


$(function () {
    Feng.initValidator("${package.Parent}", ${entity}InfoDlg.validateFields);
});
